<?php
namespace frontend\models;

use common\models\User;
use yii\base\Model;
use Yii;

/**
 * Signup form
 */
class SignupForm extends Model
{
    public $user_name;
    public $user_email;
    public $user_pass;
    public $con_user_pass;
    public $user_url;
    public $display_name;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['user_name', 'filter', 'filter' => 'trim'],
            [['user_name', 'user_pass', 'user_url'], 'required'],
            ['user_name', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
            ['user_name', 'string', 'min' => 2, 'max' => 255],

            ['user_email', 'filter', 'filter' => 'trim'],
            ['user_email', 'required'],
            ['user_email', 'email'],
            ['user_email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

            ['user_pass', 'string', 'min' => 6, 'max'=>16],
        ];
    }
    
     public function attributeLabels()
    {
        return [
            'user_id' => '用户ID',
            'user_name' => '用户名',
            'user_pass' => '密码',
            'con_user_pass' => '确认密码',
            'user_email' => '用户邮箱',
            'user_url' => '用户网站',
            'registered' => '注册时间',
            'user_status' => '帐号状态',
            'display_name' => '昵称',
        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if ($this->validate()) {
            $user = new User();
            $user->user_name = $this->user_name;
            $user->user_email = $this->user_email;
            $user->user_url = $this->user_url;
            $user->display_name = $this->display_name;
            $user->setPassword($this->user_pass);
            $user->save();
            print_r($user->errors);die;
            return $user;
        }

        return null;
    }
}
